$(document).ready(function(){
	$('.cc-days-scroll').append(createTable('2011'));
	var ms = $('.month-selector'),
		msb = $('.cc-month-box')[0],
		msh = $('.month-selector-back')[0],
		mh = $(msb).innerHeight(),
		ys = $('.year-selector'),
		ysb = $('.cc-year-box')[0],
		ysh = $('.year-selector-back')[0],
		ds = $('.cc-days-scroll')[0],
		dh = $(ds).innerHeight(),
		inch = dh / (mh - 14);
	ms.draggable({axis: 'y',containment:msb}).bind("drag", function(event, ui) {
		msh.style.top = ui.position.top + 'px';
		ds.style.top = - (ui.position.top * inch) + 'px';
		if (ui.position.top <= 7) {
			ds.style.top = '0px';
		} else if (ui.position.top >= mh - 59) {
			ds.style.top = - (dh - mh + 14) + 'px';
		}
	});
	ys.draggable({axis: 'y',containment:ysb}).bind("drag", function(event, ui) {
		ysh.style.top = ui.position.top + 'px';
	});
	var Today = {
		date: new Date(),
		month: (new Date()).getUTCMonth(),
		day:  (new Date()).getUTCDate()
	};
	var monthFirst = $('.cc-month-list').children()[Today.month],
		monthPosition = $(monthFirst).position().top - $(monthFirst).innerHeight()/2;
	ms[0].style.top = monthPosition + 'px';
	msh.style.top = monthPosition + 'px';
	ds.style.top = - (monthPosition * inch) + 'px';
});
var monthList = [
		'январь',
		'февраль',
		'март',
		'апрель',
		'май',
		'июнь',
		'июль',
		'август',
		'сентябрь',
		'октябрь',
		'ноябрь',
		'декабрь'
	];
var yearsList = [
		2002,
		2003,
		2004,
		2005,
		2006,
		2007,
		2008,
		2009,
		2010,
		2011,
		2012,
		2013
	];
function createTable(year){
	var doc = document,
		_monthList = monthList,
		_Math = Math,
		table,
		tbody,
		tr, td,
		thead,
		container = doc.createDocumentFragment(),
		days = 7,
		year, 
		month, 
		week, 
		day, 
		sday, 
		daysInMonth, 
		weeks, 
		curDay,
		end,
		Today = {
			date: new Date(),
			month: (new Date()).getUTCMonth(),
			day:  (new Date()).getUTCDate()
		};
	//for (year = 0; year < yearsList.length; year++) {
		for (month = 0; month < 12; month++) {
			table = doc.createElement('table');
			table.className = 'calendar-days';
			thead = doc.createElement('div');
			thead.className = 'month-title';
			thead.innerHTML = _monthList[month];
			tbody = doc.createElement('tbody');
			sday = getFirstDayOfMonth(year, month);
			daysInMonth = getDaysInMonth(year,month);
			weeks = _Math.ceil((daysInMonth + sday) / days);
			end = daysInMonth + sday - 1;
			for (week = 0; week < weeks; week++) {
				tr = doc.createElement('tr');
				for (day = 0; day < days; day++) {
					td = doc.createElement('td');
					curDay = week * days + day;
					if ((month === Today.month) && (curDay - sday + 1  === Today.day)) {
						td.className = 'today';
					}
					if ((!week && day < sday) || ((week === weeks - 1) && (curDay > end))) {
						td.innerHTML = '';
					} else {
						td.innerHTML = curDay - sday + 1;
					}
					tr.appendChild(td);
				}
				tbody.appendChild(tr);
			}
			table.appendChild(tbody);
			container.appendChild(thead);
			container.appendChild(table);
		}
		table = tbody = tr = td = thead = null;
		return container;
	//}
}
function getDaysInMonth(year,month) {
	return 32 - new Date(year, month, 32).getDate();
}
function getFirstDayOfMonth(year, month) {
	return new Date(year, month, 1).getUTCDay();
}